<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        
        <button @click="getSchoolList">点我一下试试</button>
        <table border="1" cellspacing="0"> 
            <tr>
                <th>省份</th>
                <th>个数</th>
            </tr>
            <tr v-for="(item,index) in schoolList" :key="index">
                <td>{{item.area}}</td>
                <td>{{item.counts}}</td>
            </tr>
        </table>
    </div>
    <script src="./js/vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                schoolList: []
            },
            methods: {
                getSchoolList() {
                    // 1.创建xhr对象
                    let xhr = new XMLHttpRequest();
                    // 2.绑定回调函数，监听事件，这里使用箭头函数是为了让this指向vue
                    xhr.onreadystatechange = () => {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            console.log('访问成功了!');
                            this.schoolList = JSON.parse(xhr.responseText).data.list

                        }
                    }
                    // 3.设置请求
                    xhr.open('GET', 'https://api.i-lynn.cn/college');
                    // 可以自定义添加请求头
                    xhr.setRequestHeader('name', 'zx');
                    // 4.发送请求
                    xhr.send();
                }
            }

        })
    </script>
</body>

</html>